//
//  LZVideoPresenter.m
//  laziz_Consumer
//
//  Created by xbnzd on 16/11/22.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZVideoPresenter.h"
#import "NetworkServiceManager.h"
#import "LZVideoDetialModel.h"
#import "LZVideoListModel.h"
#import "MJExtension.h"
#import "LZUserModel.h"
#import "LZCuisineModel.h"
#import "LZHotAreaModel.h"
#import "LZReviewCountModel.h"
#import "LAReviewModel.h"

extern NSString *customer;

static NSString *kCityList  = @"/clientbasisInfoInstall/cityList";     //城市列表
static NSString *kVideoListUrl = @"/clientsalesPromotion/listPage";    //视频列表
static NSString *kSaveShareUrl = @"/clientsalesPromotion/shareSave";   //分享次数 +1
static NSString *kVideoPlayAddUrl = @"/clientsalesPromotion/playSave"; //播放次数+1
static NSString *kVideoSearchAddUrl = @"/clientsalesPromotion/searchSave"; //搜索次数+1
static NSString *kVideoDetailURL = @"/clientsalesPromotion/info"; //视频详情
static NSString *kVideoCommentNumURL = @"/clientvideoComments/commentCount";  //评论数量统计
static NSString *kCommentListURL = @"/clientvideoComments/listPageByPromotionId"; //评论列表

@implementation LZVideoPresenter

#pragma mark -
#pragma mark 城市列表

/**
 获取城市列表.只获取一次
 
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getCityListResultSuccess:(void (^)(NSArray *))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kCityList];
    [[NetworkServiceManager manager] executGetTask:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id resultData) {
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            NSArray *arrTemp = resultData[@"data"];
            if (successBlock) {
                successBlock(arrTemp);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }

    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}


#pragma mark -
#pragma mark 视频列表

/**
 视频列表
 
 @param xdic 筛选参数,可为空
 @param pageNum 当前页数
 @param pageLimit 每页数量
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getVideoListWithParameter:(NSDictionary *)xdic PageNum:(int)pageNum PageLimit:(int)pageLimit TimeStamp:(NSTimeInterval)timeStamp ResultSuccess:(void (^)(NSArray *, int, int, int, NSTimeInterval))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    NSMutableDictionary *parameter = xdic.mutableCopy;
    
    if ([LZUserModel Instance].userToken.length > 0) {
        parameter[@"token"] = [LZUserModel Instance].userToken;
    }

    if (timeStamp > 0) {
        parameter[@"timestamp"] = [NSNumber numberWithDouble:timeStamp];
    }
    parameter[@"cpage"] = [NSNumber numberWithInt:pageNum];
    parameter[@"limit"] = [NSNumber numberWithInt:pageLimit];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kVideoListUrl];
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:parameter success:^(NSURLSessionDataTask *task, id resultData) {
        if ([self isRequestStatusCodeCorrect:resultData fail:failedBlock]) {
            int totlePageTemp = [resultData[@"data"][@"totalPages"] intValue];
            int totleCountTemp = [resultData[@"data"][@"totalCount"] intValue];
            int currentPageTemp = [resultData[@"data"][@"cPageNO"] intValue];
            NSTimeInterval currentTimeTemp  = [resultData[@"timestamp"] doubleValue];
            NSArray *arrTemp = resultData[@"data"][@"list"];
            NSMutableArray *listArrTemp = [[NSMutableArray alloc] init];
            for (int i = 0; i < arrTemp.count; i ++) {
                NSDictionary *dicTemp = arrTemp[i];
                LZVideoListModel *m = [LZVideoListModel mj_objectWithKeyValues:dicTemp];
                [listArrTemp addObject:m];
            }
            
            if (successBlock) {
                successBlock(listArrTemp,totlePageTemp,totleCountTemp,currentPageTemp,currentTimeTemp);
            }
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(failedBlock, @"网络错误,请稍后再试");
    }];
    
}


#pragma mark -
#pragma mark 视频下载

/**
 视频下载
 
 @param urlStr 下载地址
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (NSURLSessionDownloadTask *)downLoadVideoWithURLStr:(NSString *)urlStr Progress:(void (^)(NSProgress *))progressBlock ResultSuccess:(void (^)(NSURL *))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    NSString *str = [NSString stringWithFormat:@"%@%@",APIBaseImageUrl,urlStr];
   return  [[NetworkServiceManager manager] executeDownloadTask:str progress:progressBlock completion:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        if (filePath != nil) {
            if (successBlock) {
                successBlock(filePath);
            }
        }else{
            if (failedBlock) {
                failedBlock(@"视频加载失败,请稍后再试");
            }
        }
    }];
}

#pragma mark -
#pragma mark 视频分享次数++
/**
 保存分享 +1
 
 @param videoId <#videoId description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)saveShareCountWithVideoID:(NSString *)videoId ResultSuccess:(void (^)(NSInteger))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@?id=%@",customer,kSaveShareUrl,videoId];
    [[NetworkServiceManager manager] executGetTask:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id resultData) {
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            if (successBlock) {
                successBlock([resultData[@"data"] integerValue]);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}



#pragma mark -
#pragma mark 视频播放次数 +1
/**
 视频播放次数+1
 
 @param videoId <#videoId description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)videoPlayNumAddWithVideoID:(NSString *)videoId ResultSuccess:(void (^)(NSInteger))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@?id=%@",customer,kVideoPlayAddUrl,videoId];
    [[NetworkServiceManager manager] executGetTask:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            if (successBlock) {
                successBlock([resultData[@"data"] integerValue]);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}



#pragma mark -
#pragma mark 视频搜索次数+1
/**
 视频搜索次数+1
 
 @param videoId 视频id
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */

+ (void)videoSearchNumAddWithVideoID:(NSString *)videoId ResultSuccess:(void (^)(void))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@?id=%@",customer,kVideoSearchAddUrl,videoId];
    [[NetworkServiceManager manager] executGetTask:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            if (successBlock) {
                successBlock();
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }

    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}



#pragma mark -
#pragma mark 视频详情
/**
 视频详情
 
 @param videoId <#videoId description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getVideoDetailWithVideoID:(NSString *)videoId ResultSuccess:(void (^)(LZVideoDetialModel *, NSTimeInterval))successBlock ResuleFailed:(LZVideoDetailFailedBlock)failedBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kVideoDetailURL];
    NSDictionary *parameter;
    if ([LZUserModel Instance].userToken.length == 0) {
        parameter = @{
                      @"id":videoId,
                      };
    }else{
        parameter = @{
                      @"id":videoId,
                      @"token" : [LZUserModel Instance].userToken
                      };
    }
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:parameter success:^(NSURLSessionDataTask *task, id resultData) {
        
        NSTimeInterval tt = [resultData[@"timestamp"] doubleValue];
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            LZVideoDetialModel *m = [LZVideoDetialModel mj_objectWithKeyValues:resultData[@"data"]];
            if (successBlock) {
                successBlock(m,tt);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }

        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
    
    
}



#pragma mark -
#pragma mark 视频详情中评论列表

/**
 优惠视频评论统计
 
 @param videoId 视频id
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getVideoCommentNumWithVideoID:(NSString *)videoId ResultSuccess:(void (^)(id ))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@?id=%@",customer,kVideoCommentNumURL,videoId];
    
    [[NetworkServiceManager manager] executGetTask:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            LZReviewCountModel *model = [LZReviewCountModel mj_objectWithKeyValues:resultData[@"data"]];
            
            if (successBlock) {
                successBlock(model);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}






#pragma mark -
#pragma mark 视频详情中评论列表
/**
 视频评论列表
 
 @param promotionId <#promotionId description#>
 @param currentPage <#currentPage description#>
 @param pageLimit <#pageLimit description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getVideoCommentListWithPrameter:(NSDictionary*)prameter PromotionID:(NSString *)promotionId CurrentPage:(int)currentPage PageLimit:(int)pageLimit ResultSuccess:(void (^)(NSArray *,NSNumber *))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kCommentListURL];
    NSMutableDictionary *dic = prameter?[prameter mutableCopy]:[@{} mutableCopy];
    dic[@"promotionId"] = promotionId;
    dic[@"cpage"] = [NSNumber numberWithInt:currentPage];
    dic[@"limit"] = [NSNumber numberWithInt:pageLimit];
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            
            NSArray *arrResult = [[resultData objectForKey:@"data"] objectForKey:@"list"];
            NSMutableArray *arrData = [NSMutableArray array];
            for (int i = 0; i<arrResult.count; i++) {
                LAReviewModel *model = [LAReviewModel mj_objectWithKeyValues:arrResult[i]];
                [arrData addObject:model];
            }
            if (successBlock) {
                successBlock(arrData,resultData[@"timestamp"]);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}



@end


